package controller;

import java.io.IOException;
import java.sql.SQLDataException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modal.UserModal;
import dao.UsersDAO;

@WebServlet("/RegisterController")
public class RegisterController  extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RegisterController() {

    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			UsersDAO udao= new UsersDAO();
			if(udao.getUserwithEmail(request.getParameter("inputEmail"))==null)
			{
			udao.addUser(request.getParameter("inputEmail"),request.getParameter("inputPassword1"));
			//TODO maybe set a message to say login ? 
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
			else {
			//TODO set a message to say unique email needed. 
			request.getRequestDispatcher("/signup.jsp").forward(request, response);
			}
		} catch (SQLDataException e) {
			e.printStackTrace();
		} catch(Exception e){
		e.printStackTrace();
		}
	}

}
